<?php
// +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务，帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.yiovo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件，不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 萤火科技 <admin@yiovo.com>
// +----------------------------------------------------------------------
declare (strict_types=1);

namespace app\api\controller;

use app\common\model\presalesCustomer\Log;
use app\common\model\presalesCustomer\PresalesCustomer;
use app\store\model\customer\Package;

/**
 * 默认控制器
 * Class Index
 * @package app\api\controller
 */
class Index
{
    public function index()
    {
        $url = "/store";
        header("Location: $url");
        die;
    }

    public function savesign($image, $package_id)
    {
        $signature_url = saveBase64($image);
        $package = (new Package())->where(['package_id' => $package_id])->find();
        if (!$package || !$package->contract_url) {
            echo "合同文件不存在";
            die;
        }
        $package->save([
            'signature_url' => $signature_url,
            'signature_time' => time()
        ]);
        $package->generateContract();
        return 1;
    }

    public function syn()
    {
        $list = (new PresalesCustomer())->where(['is_delete' => 0])->select();
        foreach ($list as $item) {
            $last_log = (new Log())->where(['pc_id' => $item->pc_id, 'is_delete' => 0])->order('pcl_id', 'desc')->find();
            if ($last_log) {
                $item->next_log_time = strtotime($last_log->next_time);
                $item->last_log_time = strtotime($last_log->create_time);
                $item->save();
            }
        }
    }

}
